package com.example.myapplication;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.myapplication.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;

    //日历
    EditText mCalendar;
    EditText mStartTime;
    EditText mEndTime;
    EditText mDate;
    TextView textView2;
//    http://www.qishunwang.net/news_show_52933.aspx


    //    https://www.runoob.com/w3cnote/android-tutorial-edittext.html
    private Button btn_add;
    private EditText edit_one;
//    private EditText edit_one;

    void setButton() {
//        setContentView(R.layout.activity_main);
//        btn_add = (Button) findViewById(R.id.btn_add);
//        edit_one = (EditText) findViewById(R.id.edit_one);
//        btn_add.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                SpannableString spanStr = new SpannableString("imge");
//                Drawable drawable = MainActivity.this.getResources().getDrawable(R.drawable.f045);
//                drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
//                ImageSpan span = new ImageSpan(drawable,ImageSpan.ALIGN_BASELINE);
//                spanStr.setSpan(span,0,4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//                int cursor = edit_one.getSelectionStart();
//                edit_one.getText().insert(cursor, spanStr);
//            }
//        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        setButton();

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.toolbar);

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

        binding.fab.setOnClickListener(view ->
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show());

        //初始化
//        mCalendar = (EditText) findViewById(R.id.calendar);
//        mStartTime = (EditText) findViewById(R.id.startTime);
//        mEndTime = (EditText) findViewById(R.id.endTime);
        mDate = (EditText) findViewById(R.id.editTextDate);
//        按钮点击 安卓
//        textView2= (TextView) findViewById(R.id.textView2);
        textView2=  findViewById(R.id.textView2);
//        https://blog.csdn.net/qq_36444585/article/details/78159731
        Button button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(v -> {
            //具体功能
            textView2.setText(mDate.getText());
        });


//        只是个layout 我觉得是可以找到的 ，但是
//        showDateOnClick  这个可能是null 的原因
//        mDate = (EditText) findViewById(R.id.e);
//        https://blog.csdn.net/qq_39161003/article/details/84339516
        //点击
//        showDateOnClick(mCalendar);
//        这个东西是空的 ，这里去 setOnTouchListener， 就会发生 null exce，就会闪退了
        showDateOnClick(mDate);
//        发现改动小的话，重新启动还是挺快的
//        showDateOnClick(mStartTime);
//        showDateOnClick(mEndTime);
    }

    //点击打开日历，改变日期
    @SuppressLint("ClickableViewAccessibility")
    protected void showDateOnClick(final EditText editText) {
        //点击事件传递，接触，点击打开
        editText.setOnTouchListener((view, motionEvent) -> {
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                showDatePickDlg(editText);
                return true;
            }

            return false;
        });
        //改变日期
//        https://www.cnblogs.com/zhujiabin/p/5681040.html
//        焦点被 日期改变 text 获得了，现在的焦点在view ，这个view 就产生了他的 日期选择器
        editText.setOnFocusChangeListener((view, b) -> {
            if (b) {
                showDatePickDlg(editText);
            }

        });
    }

    //    http://www.qishunwang.net/news_show_52933.aspx
    //选择日期,改变文本
    protected void showDatePickDlg(final EditText editText) {
        Calendar calendar = Calendar.getInstance();
//        但是他是根据选择的时间吗， 而不是 现在的时间
        Log.i(TAG, "showDatePickDlg: calendar  " + calendar);
//        这里 getInstance 的 日期 就是现在的 ，我觉得可能是因为 context 使得 calender 的日期是选择的那个吧
//        https://blog.csdn.net/qq_39969226/article/details/88324722
//        DatePickerDialog 怎么 做到 选择的 日期
//        https://www.cnblogs.com/niray/p/3877503.html
        @SuppressLint("SetTextI18n") DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this,
                (view, year, monthOfYear, dayOfMonth)
                        -> editText.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日"),
                calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//        使用了这几个 年月日的值
        datePickerDialog.show();
    }

    //选择日期,改变文本
//    protected void showDatePickDlg(final EditText editText) {
//        Calendar calendar = Calendar.getInstance();
//        DatePickerDialog datePickerDialog = new DatePickerDialog(ApplyActivity.this, new DatePickerDialog.OnDateSetListener() {
//            @Override
//            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
//                editText.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
//            }
//        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
////        使用了这几个 年月日的值
//        datePickerDialog.show();
//    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }
}